//
//  SINAAskListPageViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2017/12/25.
//  Copyright © 2017年  androidlongs. All rights reserved.
//

#import "SINAAskListPageViewController.h"
#import "SINAAskLeaveRecordModel.h"
#import "SINAAskLeaveItemTableViewCell.h"
#import "SINAAskDetailViewController.h"
#import "SINAAskLeaveRecordModel.h"
#import "SINATecApprovalManagerDetailViewController.h"
#import "SINAStuHomeCoursItemModel.h"

@interface SINAAskListPageViewController () <UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *mainContentTableView;
@property(nonatomic,strong) NSMutableArray *dataList;
// 刷新中得状态判断，只读属性，根据状态可做一些自定义的事情
@property (nonatomic, readonly, getter=isRefreshing) BOOL refreshing;

//分页加载数据
@property(nonatomic,assign) NSInteger pageIndex;
@property(nonatomic,assign) NSInteger pageSize;


@end

@implementation SINAAskListPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    [self configNavigationItem];
    self.pageIndex = 1;
    self.pageSize = 50;
    self.mainContentTableView.delegate=self;
    self.mainContentTableView.dataSource=self;
    self.mainContentTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.mainContentTableView.backgroundColor=[UIColor colorWithHexString:@"#eeeeee"];
    self.mainContentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    
    for (int i=0; i<0; i++) {
        SINAAskLeaveRecordModel *model = [[SINAAskLeaveRecordModel alloc]init];
        [self.dataList addObject:model];
    }
    [self.mainContentTableView reloadData];
    
    //下拉刷新
    //self.mainContentTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadingNewData)];
    //上拉刷新
    //self.mainContentTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadingMoreData)];
    
    //自动更改透明度
    self.mainContentTableView.mj_header.automaticallyChangeAlpha = YES;
    
    //进入刷新状态
    //[self.mainContentTableView.mj_header beginRefreshing];
    
    [self.mainContentTableView.mj_header setBackgroundColor:[UIColor colorWithHexString:@"#eeeeee"]];
    [self.mainContentTableView.mj_footer setBackgroundColor:[UIColor colorWithHexString:@"#eeeeee"]];
    
    [self loadingSubClassLeaveAskListNetData];
    
}

//下拉刷新
-(void) loadingNewData{
    self.pageIndex=0;
//    __weak typeof(self) weakSelf = self;
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        //结束头部刷新
//        [weakSelf.mainContentTableView.mj_header endRefreshing];
//    });
    [self.mainContentTableView.mj_header endRefreshing];
    [self loadingSubClassLeaveAskListNetData];
}

//上拉加载更多
-(void) loadingMoreData{
    self.pageIndex++;
    //结束尾部刷新
    [self.mainContentTableView.mj_footer endRefreshing];
    [YPXToastView showToastViewWithText:@"暂无更多数据 " andDuration:1 andParentView:self.view];
}
-(NSMutableArray *)dataList{
    if (!_dataList) {
        _dataList = [NSMutableArray array];
    }
    return  _dataList;
}
-(void)configNavigationItem{
    self.navigationController.navigationBarHidden = NO;
    [self.view setBackgroundColor:[UIColor colorWithHexString:@"#eeeeee"]];
    self.navigationItem.title=@"申请记录";
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0/255 green:198.0/255 blue:174.0/255 alpha:0.8f];
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],
                                                                      NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    SINAAskLeaveItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SINAAskLeaveItemTableViewCell"];
    
    if (cell == nil) {
        cell= [[[NSBundle mainBundle]loadNibNamed:@"SINAAskLeaveItemTableViewCell" owner:nil options:nil] firstObject];
        
    }
    
    cell.itemClick = ^(SINAAskLeaveRecordModel *itemModel) {
        
        DLog(@"点击 详情 ");
        
        SINATecApprovalManagerDetailViewController *approvalController = [[SINATecApprovalManagerDetailViewController alloc]initWithNibName:@"SINATecApprovalManagerDetailViewController" bundle:nil];
        SINATecManagerLeaveModel *model =[[SINATecManagerLeaveModel alloc]init];
        model.uuid = itemModel.uuid;
        approvalController.model =model;
        approvalController.flag=1;
        [self.navigationController pushViewController:approvalController animated:YES];
    };
    
    SINAAskLeaveRecordModel *model= self.dataList[indexPath.row];
    cell.model=model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell ;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 165;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    NSInteger index = indexPath.row;
    
    SINAAskLeaveRecordModel *model = self.dataList[index];
    
    SINAAskDetailViewController *controller = [[SINAAskDetailViewController alloc]init];
    controller.model = model;
    [self.navigationController pushViewController:controller animated:YES];
    
}

//加载请假申请记录
-(void)loadingSubClassLeaveAskListNetData{
    
    [self startLoading];
    [self.dataList removeAllObjects];
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    
    //学生 uuid
    [dict setObject:[[SINACommonUserHelper share]getUserModel].userUid forKey:@"useruuid"];
    
    WeakSelf;
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@",baseUrl,getStudentLeaveAskListUrl] withParamters:dict success:^(id responseData) {
        
        NSString *code = responseData[@"code"];
        [weakSelf stopLoading];
        if ([code isEqualToString:@"1000"]) {
            [self.dataList removeAllObjects];
            
            NSMutableArray *result = responseData[@"content"];
            
            if (result!=nil&&result.count>0) {
                for (NSInteger i=0; i<result.count; i++) {
                    NSDictionary *dict=result[i];
                    
                    SINAAskLeaveRecordModel *model = [[SINAAskLeaveRecordModel alloc]initWithDict:dict];
                    
                    
                    [self.dataList addObject:model];
                }
                [self.mainContentTableView reloadData];
            }else{
                [YPXToastView showToastViewWithText:@"暂无请假数据 " andDuration:3 andParentView:self.mainContentTableView];
            }
            
        }else{
//            NSString *message =responseData[@"message"];
//            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
//                                                                 cancleBtn:nil];
            //[alertView showXLAlertView];
            //self.noDataLabel.hidden=NO;
            //self.mainContentTableView.hidden=YES;
            [YPXToastView showToastViewWithText:@"加载班级数据异常 " andDuration:3 andParentView:self.mainContentTableView];
        }
        
        
        
    } failure:^(NSError *error, NSInteger errCode) {
        DLog(@"加载 失败  %@ ",error.debugDescription);
        [weakSelf stopLoading];
//        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"加载数据失败" sureBtn:@"确定"
//                                                             cancleBtn:nil];
        //[alertView showXLAlertView];
        //        self.noDataLabel.hidden=NO;
        //        self.mainContentTableView.hidden=YES;
        [YPXToastView showToastViewWithText:@"加载请假数据异常 " andDuration:3 andParentView:self.mainContentTableView];
    }];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden=NO;
}
@end
